# work1线程对全局变量的改变，在work2中是否能查看修改后的结果
import time
import threading

# 定义全局变量
g_num = 0

# work1
def work1():
    # 声明g_num是一个全局变量，才能进行修改
    global g_num

    for i in range(10):
        g_num += 1
        print('work1--------',g_num)  # 10

def work2():
    print('work2-------',g_num) # 10 g_num 可以在多线程中共享

if __name__=='__main__':
    # 创建2个线程
    t1 = threading.Thread(target=work1)
    t2 = threading.Thread(target=work2)
    # 启动线程
    t1.start()
    t2.start()

    # 子线程都执行完之后 才打印g_num
    while len(threading.enumerate()) !=1:
        time.sleep(0.5)

    # 在t1和t2 线程执行完成后打印g_num
    print('g_num---------',g_num)